/**
* add to body onLoad="lineNumbersToCode()"
*/
function lineNumbersToCode() {
	var items = document.getElementsByTagName("pre");
	for( var i=0; i<items.length; i++) {
		if( items[i].className == "code") {
			addLineNumbers( items[i] );
		}
	}
}
function addLineNumbers( element ) {
	var contents = element.innerHTML.split("\n");
	var newContents = "";
	for( var i=0; i<contents.length; i++) {
		newContents += "<span class=\"lineNumber\">"+(i+1)+"</span>"+contents[i]+"\n";
	}
	//alert(newContents);
	element.innerHTML = newContents;
}









/**
* add to body onLoad="autoindex('autoindex')" where  "autoindex" is the id of the object where index will be created
*/
indexList = new Array();
function autoindex( elementId ) {
	//var element = document.getElementById(elementId);
	var bodyTag = document.getElementsByTagName("body");
	var list = searchHeadlines( bodyTag[0] );
	
	element = document.getElementById(elementId);
	
	newContents = "";
	newContents = "<ol>";
	
	for( var i=2; i<indexList.length; i++ ) {
		if( indexList[i].tagName == "H1" ) {
			if( newContents != "<ol>") newContents += "</ol>";
			newContents += "<li><a href=\"#link_"+i+"\">"+indexList[i].innerHTML+" > </a></li><ol>";
		} else {
			newContents += "<li><a href=\"#link_"+i+"\">"+indexList[i].innerHTML+" > </a></li>";
		}
		indexList[i].innerHTML = "<a name=\"link_"+i+"\">"+indexList[i].innerHTML+"</a> <a href=\"#link_2\">></a>";
	}
	
	element.innerHTML = newContents+"</ol>";
}

function searchHeadlines( element ) {

	for( var i=0; i<element.childNodes.length; i++) {
		//if( i<2) alert(element.childNodes[i].tagName);
		if( element.childNodes[i].tagName == "H1") {
			indexList.push( element.childNodes[i]);
		} else if( element.childNodes[i].tagName == "H2") {
			indexList.push( element.childNodes[i]);
		} else {
			searchHeadlines( element.childNodes[i] );
		}
	}
}












/**
* parses all code elements to PHP code.
* Usage: Add to body onLoad="codeParser();"
*/


function codeParser() {
	var items = document.getElementsByTagName("pre");
	for( var i=0; i<items.length; i++) {
		if( items[i].className == "code") {
			items[i].innerHTML = parseCode( items[i].innerHTML );
		}
	}
}

areaTags = new Array();
areaTags.push({name:"php", start:"&lt;?php", end:"?&gt;", class:"php", selfClass:"php_tag", notIn:new Array("string","string2","comment")});
areaTags.push({name:"comment", start:"/*", end:"*/", class:"comment", selfClass:false, notIn:new Array("string","string2")});
areaTags.push({name:"string", start:"\"", end:"\"", class:"php_string", escapeChar:'\\', selfClass:false, notIn:new Array("string2")});
areaTags.push({name:"string2", start:"'", end:"'", class:"php_string", escapeChar:'\\', selfClass:false, notIn:new Array("string")});

keys = new Array();
keys.push({tag:"class", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"abstract", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"final", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"implements", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"static", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"interface", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"protected", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"private", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"public", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"new ", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"extends", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"true", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"false", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"function", class:"php_meth", mustBeIn:new Array("php") });
keys.push({tag:"if", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"else", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"elseif", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:"array", class:"php_meth", mustBeIn:new Array("php") });
keys.push({tag:"define", class:"php_meth", mustBeIn:new Array("php") });
keys.push({tag:"NULL", class:"php_meth", mustBeIn:new Array("php") });
keys.push({tag:"foreach", class:"php_func", mustBeIn:new Array("php") });
keys.push({tag:" as ", class:"php_func", mustBeIn:new Array("php") });

lineTags = new Array();
lineTags.push({tag:"@orm", class:"orm", mustBeIn:new Array("comment"), endsWith:new Array("\n") });
lineTags.push({tag:"@param", class:"annotation", mustBeIn:new Array("comment"), endsWith:new Array("\n") });
lineTags.push({tag:"@return", class:"annotation", mustBeIn:new Array("comment"), endsWith:new Array("\n") });
lineTags.push({tag:"//", class:"comment", mustBeIn:new Array(), endsWith:new Array("\n") });
lineTags.push({tag:"$", class:"php_var", mustBeIn:new Array("php"), endsWith:new Array(" ",";","-","]","[","{",",","(",")",";") });

function parseCode( code ) {
	var codeLength = code.length;
	
	var areaTagsLength = areaTags.length;
	var keysLength = keys.length;
	var lineTagsLength = lineTags.length;
	var inTags = new Array();
	var inLineTag = -1;
	
	for( var i=0; i<areaTagsLength; i++) {
		areaTags[i].startChar = areaTags[i].start.charAt(0);
		areaTags[i].endChar = areaTags[i].end.charAt(0);
		inTags[areaTags[i].name] = 0;
	}
	
	for( var i=0; i<keysLength; i++) {
		keys[i].char = keys[i].tag.charAt(0);
	}
	
	for( var i=0; i<lineTagsLength; i++) {
		lineTags[i].char = lineTags[i].tag.charAt(0);
	}
	
	//var debug = "";
	var newCode = "";
	
	for( var charNr=0; charNr<codeLength; charNr++ ) {
		char = code.charAt(charNr);
		found = false;
		
		
		// PARSING LINE TAGS
		if( inLineTag == -1 ) {
			for( var i=0; i<lineTagsLength; i++) {
				// checking for not in tags
				var skip = false;
				for( var s=0; s<lineTags[i].mustBeIn.length; s++) {
					if( inTags[lineTags[i].mustBeIn[s]] == 0 )
						skip = true;
				}
				
				// skipping if needed
				if( skip == true) 
					continue;
				
				if( char == lineTags[i].char && code.substr(charNr,lineTags[i].tag.length) == lineTags[i].tag ) {
					//debug += "in tag "+keys[i].tag+"<br />";
					inLineTag = i;
					newCode += "<span class=\""+lineTags[i].class+"\">"+lineTags[i].tag;
					charNr += lineTags[i].tag.length-1;
					found = true;
					break;
				}
			}
		} else {
			found = true;
			for( var i=0; i<lineTags[inLineTag].endsWith.length; i++) {
				if( char == lineTags[inLineTag].endsWith[i] ) {
					inLineTag = -1;
					newCode += "</span>";
					break;
				}
			}
			newCode += char;
		}
		if( found == true ) continue;
		// END PARSING LINE TAGS
		
		
		
		
		// PARSING AREA TAGS
		for( var i=0; i<areaTagsLength; i++) {
			
			// checking for not in tags
			var skip = false;
			for( var s=0; s<areaTags[i].notIn.length; s++) {
				if( inTags[areaTags[i].notIn[s]] > 0 )
					skip = true;
			}
			
			// skipping if needed
			if( skip == true) 
				continue;
		
			
			if( inTags[areaTags[i].name] == 0 && char == areaTags[i].startChar && code.substr(charNr,areaTags[i].start.length) == areaTags[i].start ) {
				inTags[areaTags[i].name]++;
				//debug += "in tag "+areaTags[i].name+"<br />";
				newCode += "<span class=\""+areaTags[i].class+"\">";
				if( areaTags[i].selfClass == false ) {
					newCode += areaTags[i].start;
				} else {
					newCode += "<span class=\""+areaTags[i].selfClass+"\">"+areaTags[i].start+"</span>";
				}
				
				charNr += areaTags[i].start.length-1;
				found = true;
			
			} else if( inTags[areaTags[i].name] > 0 && char == areaTags[i].endChar && code.substr(charNr,areaTags[i].end.length) == areaTags[i].end && code.charAt(charNr-1) != areaTags[i].escapeChar) {
				
				
				
				inTags[areaTags[i].name]--;
				//debug += "ended tag "+areaTags[i].name+"<br />";
				
				if( areaTags[i].selfClass == false ) {
					newCode += areaTags[i].end;
				} else {
					newCode += "<span class=\""+areaTags[i].selfClass+"\">"+areaTags[i].end+"</span>";
				}
				newCode += "</span>";
				
				charNr += areaTags[i].start.length-1;
				found = true;
				
			}
		}
		
		if( found == true ) continue;
		// END PARSING AREA TAGS
		
		
		
		
		// PARSING KEY TAGS
		for( var i=0; i<keysLength; i++) {
			if( char == keys[i].char && code.substr(charNr,keys[i].tag.length) == keys[i].tag ) {
				//debug += "in tag "+keys[i].tag+"<br />";
				newCode += "<span class=\""+keys[i].class+"\">"+keys[i].tag+"</span>";
				charNr += keys[i].tag.length-1;
				found = true;
				break;
			}
		}
		if( found == true ) continue;
		// END PARSING KEY TAGS
		
		

		
		newCode += char;	
	}
	
	//newCode += "<br />DEBUG:<br />"+debug+"<br />";
	return newCode;
}